﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace eVoting
{
    public partial class CreatePoll : System.Web.UI.Page
    {
        DBManager db;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBManager();

            SDDay.Items.Add("Day");
            SDDay.Items.Add("1");
            SDDay.Items.Add("2");
            SDDay.Items.Add("3");
            SDDay.Items.Add("4");
            SDDay.Items.Add("5");
            SDDay.Items.Add("6");
            SDDay.Items.Add("7");
            SDDay.Items.Add("8");
            SDDay.Items.Add("9");
            SDDay.Items.Add("10");
            SDDay.Items.Add("11");
            SDDay.Items.Add("12");
            SDDay.Items.Add("13");
            SDDay.Items.Add("14");
            SDDay.Items.Add("15");
            SDDay.Items.Add("16");
            SDDay.Items.Add("17");
            SDDay.Items.Add("18");
            SDDay.Items.Add("19");
            SDDay.Items.Add("20");
            SDDay.Items.Add("21");
            SDDay.Items.Add("22");
            SDDay.Items.Add("23");
            SDDay.Items.Add("24");
            SDDay.Items.Add("25");
            SDDay.Items.Add("26");
            SDDay.Items.Add("27");
            SDDay.Items.Add("28");
            SDDay.Items.Add("29");
            SDDay.Items.Add("30");
            SDDay.Items.Add("31");


            EDDay.Items.Add("Day");
            EDDay.Items.Add("1");
            EDDay.Items.Add("2");
            EDDay.Items.Add("3");
            EDDay.Items.Add("4");
            EDDay.Items.Add("5");
            EDDay.Items.Add("6");
            EDDay.Items.Add("7");
            EDDay.Items.Add("8");
            EDDay.Items.Add("9");
            EDDay.Items.Add("10");
            EDDay.Items.Add("11");
            EDDay.Items.Add("12");
            EDDay.Items.Add("13");
            EDDay.Items.Add("14");
            EDDay.Items.Add("15");
            EDDay.Items.Add("16");
            EDDay.Items.Add("17");
            EDDay.Items.Add("18");
            EDDay.Items.Add("19");
            EDDay.Items.Add("20");
            EDDay.Items.Add("21");
            EDDay.Items.Add("22");
            EDDay.Items.Add("23");
            EDDay.Items.Add("24");
            EDDay.Items.Add("25");
            EDDay.Items.Add("26");
            EDDay.Items.Add("27");
            EDDay.Items.Add("28");
            EDDay.Items.Add("29");
            EDDay.Items.Add("30");
            EDDay.Items.Add("31");


            SDMonth.Items.Add("Month");
            SDMonth.Items.Add("1");
            SDMonth.Items.Add("2");
            SDMonth.Items.Add("3");
            SDMonth.Items.Add("4");
            SDMonth.Items.Add("5");
            SDMonth.Items.Add("6");
            SDMonth.Items.Add("7");
            SDMonth.Items.Add("8");
            SDMonth.Items.Add("9");
            SDMonth.Items.Add("10");
            SDMonth.Items.Add("11");
            SDMonth.Items.Add("12");

            EDMonth.Items.Add("Month");
            EDMonth.Items.Add("1");
            EDMonth.Items.Add("2");
            EDMonth.Items.Add("3");
            EDMonth.Items.Add("4");
            EDMonth.Items.Add("5");
            EDMonth.Items.Add("6");
            EDMonth.Items.Add("7");
            EDMonth.Items.Add("8");
            EDMonth.Items.Add("9");
            EDMonth.Items.Add("10");
            EDMonth.Items.Add("11");
            EDMonth.Items.Add("12");


            System.Collections.Generic.List <StudyGroup> StudyGroups = db.getAllStudyGroups();

            StudyGroupDropDown.Items.Add("Study Group");

            for (int i = 0; i < StudyGroups.Count; i++)
            {
                StudyGroupDropDown.Items.Add(StudyGroups[i].Faculty1.name + "," + StudyGroups[i].year);
            }

            DateTime dt = DateTime.Now;


            SDYear.Items.Add("Year");
            EDYear.Items.Add("Year");
            for(int i = 1980 ; i<=dt.Year ;i++)
            {
                int x = 1980 + (dt.Year - i);
                SDYear.Items.Add(x.ToString());
                EDYear.Items.Add(x.ToString());
            }
        }

        protected void AddBtn_Click(object sender, EventArgs e)
        {
            DateTime Start = new DateTime(int.Parse(SDYear.SelectedValue), int.Parse(SDMonth.SelectedValue),int.Parse(SDDay.SelectedValue));
            DateTime End = new DateTime(int.Parse(EDYear.SelectedValue), int.Parse(EDMonth.SelectedValue), int.Parse(EDDay.SelectedValue));
            Poll p = new Poll();
            if(End.CompareTo(Start)<0)
            {
                ErrorLbl.Visible = true;
            }
            else if (NameTB.Text != "" && DscTB.Text != "" && SDYear.SelectedValue != "Year" && SDMonth.SelectedValue != "Month" && SDDay.SelectedValue != "Day" && EDYear.SelectedValue != "Year" && EDMonth.SelectedValue != "Month" && EDDay.SelectedValue != "Day")
            {
                p  = db.createNewPoll(NameTB.Text, DscTB.Text, Start , End );
                String[] SGinfo = StudyGroupDropDown.SelectedValue.Split(',');
                Faculty f = db.getFacultyByName(SGinfo[0]);
                StudyGroup SG =  db.getStudyGroup(f.ID,int.Parse(SGinfo[1]));
                db.assignPollToStudyGroup(p.ID,SG.ID);
                ErrorLbl.Visible = true;
                ErrorLbl.Text = "Added";
            }
            else
            {
                ErrorLbl.Visible = true;
            }
        }
    }
}
